home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / usenet / sources / volume90 / aplictns / route_10 / part03 / pcbltv.c next >
C/C++ Source or Header  |  1990-04-23  |  14KB  |  499 lines

  1. /*
  2. ** printed circuit board displayer, Copyright (C) Randy Nevin 1989.
  3. **
  4. ** you may give this software to anyone, make as many copies as you like, and
  5. ** post it on public computer bulletin boards and file servers. you may not
  6. ** sell it or charge any fee for distribution (except for media and postage),
  7. ** remove this comment or the copyright notice from the code, or claim that
  8. ** you wrote this code or anything derived from it. you may modify the code as
  9. ** much as you want (please document clearly with comments, and maintain the
  10. ** coding style), but programs which are derived from this one are subject to
  11. ** the conditions stated here. i am providing this code so that people can
  12. ** learn from it, so if you distribute it, please include source code, not
  13. ** just executables. contact me to report bugs or suggest enhancements; i do
  14. ** not guarantee support, but i will make an effort in good faith to help you,
  15. ** and i want to act as a central clearing house for future versions. you
  16. ** should contact me before undertaking a significant development effort, to
  17. ** avoid reinventing the wheel. if you come up with an enhancement you
  18. ** consider particularly useful, i would appreciate being informed so that it
  19. ** can be incorporated in future versions. my address is: Randy Nevin,
  20. ** 1731 211th PL NE, Redmond, WA 98053. this code is available directly from
  21. ** the author; just send a floppy and a self-addressed floppy mailer with
  22. **
  23. ** HISTORY
  24. ** (name        date        description)
  25. ** ----------------------------------------------------
  26. ** randy nevin        2/1/89        initial version
  27. ** randy nevin        2/11/89        released version 1.00
  28. ** udi finkelstein  2/26/90     amiga-ized. compacted bitmaps.
  29. */
  30.  
  31. #include <stdio.h>
  32.  
  33. #include "pcbview.h"
  34.  
  35. struct Screen *screen;
  36. struct Window *window;
  37. struct RastPort *rastp;
  38. struct IntuitionBase *IntuitionBase;
  39. struct GfxBase *GfxBase;
  40. struct IntuiMessage *IM;
  41. ULONG class;
  42. USHORT code;
  43.  
  44. extern struct Screen *OpenScreen();
  45. extern struct Window *OpenWindow();
  46. extern struct Library *OpenLibrary();
  47. extern void CloseLibrary(),CloseScreen(),CloseWindow();
  48. extern struct Message *GetMsg();
  49.  
  50. #define MENUCODE(m,i,s) (SHIFTMENU(m)|SHIFTITEM(i)|SHIFTSUB(s))
  51.  
  52. extern char *strrchr();
  53.  
  54. #include "cell.h"
  55.  
  56. /*
  57.  * colors of objects
  58.  */
  59. #define HOLC    0x2    /* hole color; scarlet            */
  60. #define FRSD    0x1    /* frontside trace color; bright blue    */
  61. #define BKSD    0x4    /* backside trace color; light blue    */
  62. #define EDGE    0x7    /* edge color; yellow            */
  63.  
  64. /* screen limits */
  65. #define MINHORZ    0    /* left-most pixel    */
  66. #define MAXHORZ    639    /* right-most pixel    */
  67. #define MINVERT    15    /* top-most pixel    */
  68. #define MAXVERT    511    /* bottom-most pixel    */
  69.  
  70. static int sides = 3; /* 0=holes only, 1=front only, 2=back only, 3=all */
  71.  
  72. #define MAXZOOM    3    /* maximum zoom number; minimum is 0 */
  73.  
  74. #define ZOOM0    3    /* 3x3 pixels per cell        */
  75. #define ZOOM1    6    /* 6x6 pixels per cell        */
  76. #define ZOOM2    10    /* 10x10 pixels per cell    */
  77. #define ZOOM3    18    /* 18x18 pixels per cell    */
  78.  
  79. static int zoom = 1; /* 0=3x3, 1=6x6, 2=10x10, 3=18x18 */
  80. static int zoomsize[MAXZOOM+1] = { ZOOM0, ZOOM1, ZOOM2, ZOOM3 };
  81.  
  82. /* current lower-left position */
  83. static int Xrow = 0;
  84. static int Xcol = 0;
  85.  
  86. int justboard = 1; /* only need the board data structure */
  87.  
  88. /* board dimensions */
  89. extern int Nrows;
  90. extern int Ncols;
  91.  
  92. extern void InitBoard();
  93. extern long GetCell();
  94. extern void SetCell();
  95. extern int GetMode();
  96. extern void SetMode();
  97. extern void Dot();
  98.  
  99. void main();
  100. static void doedges();
  101. static void doboard();
  102. static void map();
  103. static void plot();
  104.  
  105. void main ( argc, argv ) /* input routed board, display it on the screen */
  106. int argc;
  107. char *argv[];
  108. {
  109. char *self, *p;
  110. int r, c, rp, cp, i1, i2, i3, i4;
  111. FILE *fp;
  112. long x;
  113.  
  114.     printf( "Copyright (C) Randy Nevin, 1989. Version 1.00\n" );
  115.     printf( "See source code for rights granted.\n\n" );
  116.     printf( "Amiga Port by Udi Finkelstein\n" );
  117.     self = argv[0];
  118.     /* get rid of initial part of path */
  119.     if ((p = strrchr( self, '/' )) || (p = strrchr( self, ':' )))
  120.         self = ++p;
  121.     if (argc != 2) { /* need infile */
  122.         fprintf( stderr, "usage: %s infile\n", self );
  123.         exit( -1 );
  124.     }
  125.     if (!(fp = fopen( argv[1], "r" ))) {
  126.         fprintf( stderr, "can't open %s\n", argv[1] );
  127.         exit( -1 );
  128.     }
  129.     /* fetch the board dimensions */
  130.     if ((rp = getc( fp )) == EOF || (cp = getc( fp )) == EOF) {
  131.         fprintf( stderr, "premature eof\n" );
  132.         exit( -1 );
  133.     }
  134.     Nrows = (rp & 0xFF) | ((cp << 8) & 0xFF00);
  135.     if ((rp = getc( fp )) == EOF || (cp = getc( fp )) == EOF) {
  136.         fprintf( stderr, "premature eof\n" );
  137.         exit( -1 );
  138.     }
  139.     Ncols = (rp & 0xFF) | ((cp << 8) & 0xFF00);
  140.     InitBoard(); /* allocate memory for data structures */
  141.     for (r = 0; r < Nrows; r++) { /* read in the board, row by column */
  142.         for (c = 0; c < Ncols; c++) {
  143.             /* first, do frontside */
  144.             if ((i1 = getc( fp )) == EOF
  145.                 || (i2 = getc( fp )) == EOF
  146.                 || (i3 = getc( fp )) == EOF
  147.                 || (i4 = getc( fp )) == EOF) {
  148.                 fprintf( stderr, "premature eof\n" );
  149.                 exit( -1 );
  150.                 }
  151.             x = (long)i1 | (((long)i2) << 8)
  152.                 | (((long)i3) << 16) | (((long)i4) << 24);
  153.             SetCell( r, c, TOP, x );
  154.             /* then do backside */
  155.             if ((i1 = getc( fp )) == EOF
  156.                 || (i2 = getc( fp )) == EOF
  157.                 || (i3 = getc( fp )) == EOF
  158.                 || (i4 = getc( fp )) == EOF) {
  159.                 fprintf( stderr, "premature eof\n" );
  160.                 exit( -1 );
  161.                 }
  162.             x = (long)i1 | (((long)i2) << 8)
  163.                 | (((long)i3) << 16) | (((long)i4) << 24);
  164.             SetCell( r, c, BOTTOM, x );
  165.             }
  166.         }
  167.     /* tell user what commands are available */
  168. /*
  169.     printf( "\t0   = holes only\n" );
  170.     printf( "\t1   = holes and top traces\n" );
  171.     printf( "\t2   = holes and bottom traces\n" );
  172.     printf( "\t3   = holes and all traces\n" );
  173.     printf( "\tz/Z = zoom one level / maximum zoom\n" );
  174.     printf( "\ts/S = shrink one level / minimum shrink\n" );
  175.     printf( "\tl/L = move left by one / move left by ten\n" );
  176.     printf( "\tr/R = move right by one / move right by ten\n" );
  177.     printf( "\tu/U = move up by one / move up by ten\n" );
  178.     printf( "\td/D = move down by one / move down by ten\n" );
  179.     printf( "\tany other key exits the program\n" );
  180.     printf( "\nPress ENTER to continue, or ^C to exit " );
  181. */
  182.  
  183.     if ((GfxBase = (struct GfxBase *)OpenLibrary(
  184.         "graphics.library",33L)) == NULL) {
  185.             printf("Can't open gfx!\n");
  186.             exit(10);
  187.     }
  188.  
  189.     if ((IntuitionBase = (struct IntuitionBase *)OpenLibrary(
  190.         "intuition.library",33L)) == NULL) {
  191.             printf("Can't open intuition!\n");
  192.             CloseLibrary(GfxBase);
  193.             exit(10);
  194.     }
  195.  
  196.     if ((screen = OpenScreen(&NewScreenStructure)) == NULL) {
  197.         printf("Can't open screen!\n");
  198.         CloseLibrary(IntuitionBase);
  199.         CloseLibrary(GfxBase);
  200.         exit(10);
  201.     }
  202.  
  203.     NewWindowStructure1.Screen = screen;
  204.  
  205.     if ((window = OpenWindow(&NewWindowStructure1)) == NULL) {
  206.         printf("Can't open window!\n");
  207.         CloseScreen(screen);
  208.         CloseLibrary(IntuitionBase);
  209.         CloseLibrary(GfxBase);
  210.         exit(10);
  211.     }
  212.  
  213.     rastp = window->RPort;
  214.     SetMenuStrip(window,&MenuList1);
  215.     LoadRGB4(&screen->ViewPort,Palette,(long)PaletteColorCount);
  216.     SetDrMd(rastp, JAM1);
  217.  
  218.     doedges(); /* display board edges */
  219.     doboard(); /* display the board */
  220.     for (;;) { /* process until unrecognized keystroke */
  221.  
  222.         Wait (1L << (window->UserPort->mp_SigBit));
  223.         while((IM = (struct IntuiMessage *)
  224.                 GetMsg(window->UserPort)) != NULL) {
  225.             code = IM->Code;
  226.             class = IM->Class;
  227.             ReplyMsg(IM);
  228.             switch (class) {
  229.                 case CLOSEWINDOW:
  230.                     CloseWindow(window);
  231.                     CloseScreen(screen);
  232.                     CloseLibrary(IntuitionBase);
  233.                     CloseLibrary(GfxBase);
  234.                     exit(10);
  235.                 case MENUPICK:
  236.                     switch (code) {
  237.                         case MENUCODE(0,0,NOSUB): /* just show holes */
  238.                             if (sides == 0)
  239.                                 continue;
  240.                             sides = 0;
  241.                             break;
  242.                         case MENUCODE(0,1,NOSUB): /* show holes and top-side traces */
  243.                             if (sides == 1)
  244.                                 continue;
  245.                             sides = 1;
  246.                             break;
  247.                         case MENUCODE(0,2,NOSUB): /* show holes and bottom-side traces */
  248.                             if (sides == 2)
  249.                                 continue;
  250.                             sides = 2;
  251.                             break;
  252.                         case MENUCODE(0,3,NOSUB): /* show holes and all traces */
  253.                             if (sides == 3)
  254.                                 continue;
  255.                             sides = 3;
  256.                             break;
  257.                         case MENUCODE(2,1,NOSUB): /* zoom to the limit */
  258.                             if (zoom == MAXZOOM)
  259.                                 continue;
  260.                             zoom = MAXZOOM;
  261.                             break;
  262.                         case MENUCODE(2,0,NOSUB): /* zoom by one */
  263.                             if (zoom == MAXZOOM)
  264.                                 continue;
  265.                             zoom++;
  266.                             break;
  267.                         case MENUCODE(2,3,NOSUB): /* shrink to the limit */
  268.                             if (zoom == 0)
  269.                                 continue;
  270.                             zoom = 0;
  271.                             break;
  272.                         case MENUCODE(2,2,NOSUB): /* shrink by one */
  273.                             if (zoom == 0)
  274.                                 continue;
  275.                             zoom--;
  276.                             break;
  277.                         case MENUCODE(1,1,NOSUB): /* left by 10 */
  278.                             if (Xcol == 0)
  279.                                 continue;
  280.                             if (Xcol <= 10)
  281.                                 Xcol = 0;
  282.                             else
  283.                                 Xcol -= 10;
  284.                             break;
  285.                         case MENUCODE(1,0,NOSUB): /* left by one */
  286.                             if (Xcol == 0)
  287.                                 continue;
  288.                             Xcol--;
  289.                             break;
  290.                         case MENUCODE(1,3,NOSUB): /* right by 10 */
  291.                             if (Xcol == Ncols-1)
  292.                                 continue;
  293.                             if (Xcol >= Ncols-11)
  294.                                 Xcol = Ncols-1;
  295.                             else
  296.                                 Xcol += 10;
  297.                             break;
  298.                         case MENUCODE(1,2,NOSUB): /* right by one */
  299.                             if (Xcol == Ncols-1)
  300.                                 continue;
  301.                             Xcol++;
  302.                             break;
  303.                         case MENUCODE(1,5,NOSUB): /* up by 10 */
  304.                             if (Xrow == Nrows-1)
  305.                                 continue;
  306.                             if (Xrow >= Nrows-11)
  307.                                 Xrow = Nrows-1;
  308.                             else
  309.                                 Xrow += 10;
  310.                             break;
  311.                         case MENUCODE(1,4,NOSUB): /* up by one */
  312.                             if (Xrow == Nrows-1)
  313.                                 continue;
  314.                             Xrow++;
  315.                             break;
  316.                         case MENUCODE(1,7,NOSUB): /* down by 10 */
  317.                             if (Xrow == 0)
  318.                                 continue;
  319.                             if (Xrow <= 10)
  320.                                 Xrow = 0;
  321.                             else
  322.                                 Xrow -= 10;
  323.                             break;
  324.                         case MENUCODE(1,6,NOSUB): /* down by one */
  325.                             if (Xrow == 0)
  326.                                 continue;
  327.                             Xrow--;
  328.                             break;
  329.                     }
  330.             }
  331.         }
  332.         ClearPCB();
  333.         doedges(); /* display board edges */
  334.         doboard(); /* display the board */
  335.     }
  336. }
  337.  
  338. /*
  339.  * display the board edges
  340.  */
  341. static void doedges ()
  342. {
  343. int r1, c1, r2, c2, i, z;
  344. long xr1, xc1, xr2, xc2;
  345.  
  346.     z = zoomsize[zoom];
  347.     /* first, calculate their virtual screen positions */
  348.     r1 = MAXVERT+(Xrow*z); /* bottom edge */
  349.     c1 = MINHORZ-(Xcol*z); /* left edge */
  350.     r2 = MAXVERT-1-((Nrows-Xrow)*z); /* top edge */
  351.     c2 = MINHORZ+1+((Ncols-Xcol)*z); /* right edge */
  352.  
  353.     SetAPen(rastp,(long)EDGE);
  354.  
  355.     xr1 = r1; xr2 = r2;
  356.     if (r1 < MINVERT) xr1 = MINVERT;
  357.     if (r1 > MAXVERT) xr1 = MAXVERT;
  358.     if (r2 < MINVERT) xr2 = MINVERT;
  359.     if (r2 > MAXVERT) xr2 = MAXVERT;
  360.  
  361.     xc1 = c1; xc2 = c2;
  362.     if (c1 < MINHORZ) xc1 = MINHORZ;
  363.     if (c1 > MAXHORZ) xc1 = MAXHORZ;
  364.     if (c2 < MINHORZ) xc2 = MINHORZ;
  365.     if (c2 > MAXHORZ) xc2 = MAXHORZ;
  366.  
  367.     if (r1 >= MINVERT && r1 <= MAXVERT) { /* draw bottom edge */
  368.         Move(rastp,xc1,(long)r1);
  369.         Draw(rastp,xc2,(long)r1);
  370.     }
  371.  
  372.     if (c1 >= MINHORZ && c1 <= MAXHORZ) { /* draw left edge */
  373.         Move(rastp,(long)c1,xr1);
  374.         Draw(rastp,(long)c1,xr2);
  375.     }
  376.  
  377.     if (r2 >= MINVERT && r2 <= MAXVERT) { /* draw top edge */
  378.         Move(rastp,xc1,(long)r2);
  379.         Draw(rastp,xc2,(long)r2);
  380.     }
  381.  
  382.     if (c2 >= MINHORZ && c2 <= MAXHORZ) { /* draw right edge */
  383.         Move(rastp,(long)c2,xr1);
  384.         Draw(rastp,(long)c2,xr2);
  385.     }
  386. }
  387.  
  388. /*
  389.  * display the board on the screen, row by column
  390.  */
  391. static void doboard ()
  392. {
  393. int r, c, rp, cp, rpd, cpd, z;
  394. long x, y;
  395.  
  396.     z = zoomsize[zoom];
  397.     rpd = MINVERT+z; /* top-most plottable row */
  398.     cpd = MAXHORZ-z; /* right-most plottable column */
  399.     for (r = Xrow, rp = MAXVERT-1; r < Nrows && rp >= rpd; r++, rp -= z) {
  400.         for (c = Xcol, cp = MINHORZ+1; c < Ncols && cp <= cpd;
  401.                 c++, cp += z) {
  402.             x = GetCell( r, c, TOP );
  403.             y = GetCell( r, c, BOTTOM );
  404.             if (x || y) /* only map if something is there */
  405.                 map( rp, cp, x, y );
  406.             }
  407.         }
  408. }
  409.  
  410. struct templates { /* group the bit templates for an object */
  411.     long t;                /* the object type */
  412.     struct Image im[4];    /* tiny, small, medium, large zoom templates */
  413. };
  414.  
  415. extern struct templates y1[];  /* hole templates        */
  416. extern struct templates y2[];  /* hole-related templates    */
  417. extern struct templates y3[];  /* non-hole-related templates    */
  418.  
  419. extern int z1;  /* number of hole types            */
  420. extern int z2;  /* number of hole-related types        */
  421. extern int z3;  /* number of non-hole-related types    */
  422.  
  423. #define domap1(v,c)    { for (i = 0; i < z1; i++) \
  424.                 if (v & (y1[i].t)) \
  425.                         plot( rp, cp, &(y1[i].im[zoom]), c );\
  426.                 } \
  427.  
  428. #define domap2(v,c)    { for (i = 0; i < z2; i++) \
  429.                 if (v & (y2[i].t)) \
  430.                         plot( rp, cp, &(y2[i].im[zoom]), c );\
  431.                 } \
  432.  
  433. #define domap3(v,c)    { for (i = 0; i < z3; i++) \
  434.                 if (v & (y3[i].t)) \
  435.                         plot( rp, cp, &(y3[i].im[zoom]), c );\
  436.                 } \
  437.  
  438. /*
  439.  * map a cell
  440.  */
  441. static void map ( rp, cp, v0, v1 )
  442. int rp, cp;
  443. long v0, v1;
  444. {
  445. int i;
  446.  
  447.     if (v0 & HOLE) {
  448.         domap1( v0, HOLC ); /* plot the hole */
  449.         if (v1 && (sides & 2)) /* plot backside? */
  450.             domap2( v1, BKSD );
  451.         if (v0 && (sides & 1)) /* plot frontside? */
  452.             domap2( v0, FRSD );
  453.     } else {
  454.         if (v1 && (sides & 2)) /* plot backside? */
  455.             domap3( v1, BKSD );
  456.         if (v0 && (sides & 1)) /* plot frontside? */
  457.             domap3( v0, FRSD );
  458.     }
  459. }
  460.  
  461. /*
  462.  * plot a template
  463.  */
  464.  
  465. /*
  466.  * This is the old routine, modified to use a 'struct Image *Image'
  467.  * instead of the old obj[ZOOM][ZOOM] structure which was larger,
  468.  * slower, but portable.
  469.  
  470. static void plot ( rp, cp, im, color )
  471. int rp, cp;
  472. struct Image *im;
  473. int color;
  474. {
  475. int r, c, x=0;
  476.  
  477.     for (r = 0; r < im->Height; r++)
  478.         for (c = 0; c < im->Width; c++) {
  479.             if (c == 16) x++;
  480.             if ((0x8000 >> (c&15)) & (im->ImageData)[r+x])
  481.                 Dot( color, rp-r, cp+c );
  482.         }
  483. }
  484. */
  485.  
  486. /*
  487.  * This is the new plot() routine, which use the Image structure,
  488.  * and directly calls the blitter.
  489.  */
  490. static void plot ( rp, cp, im, color )
  491. int rp, cp;
  492. struct Image *im;
  493. int color;
  494. {
  495.     SetAPen(rastp, (long)color);
  496.     BltPattern(rastp, im->ImageData, (long)cp, (long)(rp - im->Height + 1),
  497.         (long)(cp + im->Width - 1), (long)rp, (im->Width < 17) ? 2L : 4L);
  498. }
  499.